import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { getUserInfo } from "../../api/User";
const userStore = createSlice({
    name: 'userStore',
    initialState: {
        userInfo: {},
        loading: false
    },
    reducers: {

    },
    extraReducers: (builder) => {
        builder
            .addCase(fetchUserInfo.pending, (state) => {
                state.loading = true;
            })
            .addCase(fetchUserInfo.fulfilled, (state, action) => {
                state.userInfo = action.payload;
                state.loading = false;
                console.log('redux用户信息:', action.payload);
            })
            .addCase(fetchUserInfo.rejected, (state) => {
                state.loading = false;
            });
    }

})
// 创建异步thunk获取用户信息
const fetchUserInfo = createAsyncThunk('userStore/fetchUserInfo',
    async () => {
        const res = await getUserInfo();
        return res.result;
    }
);
export default userStore.reducer;
export { fetchUserInfo };
